package com.xiaobaibai.es;

import com.alibaba.fastjson.JSON;
import com.xiaobaibai.vo.BlogArticle;
import com.xiaobaibai.bo.BlogEs;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;

@Service
@Slf4j
public class EsServiceImpl implements IEsService{

    @Autowired
    @Qualifier("esClient")
    private RestHighLevelClient client;

    @Override
    public boolean addDoc(BlogArticle blogArticle) {

        BlogEs blogEs=null;
        try {
            blogEs=new BlogEs(blogArticle.getId(),blogArticle.getTitle(),blogArticle.getLabel_id(),
                    blogArticle.getImage(),new String[]{blogArticle.getTag()},blogArticle.getOverview(),
                    DateUtils.parseDate(blogArticle.getCreate_time(),"yyyy-MM-dd HH:mm:ss"));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        IndexRequest indexRequest=new IndexRequest("blog");
        indexRequest.id(blogArticle.getId().toString());
        indexRequest.source(JSON.toJSONString(blogEs), XContentType.JSON);
        IndexResponse indexResponse=null;
        try {
            indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String status=indexResponse.status().toString();
        if("CREATED".equals(status))
            return true;

        return false;
    }

    @Override
    public boolean updateDoc(BlogArticle blogArticle) {

        BlogEs blogEs=null;
        try {
            blogEs=new BlogEs(blogArticle.getId(),blogArticle.getTitle(),blogArticle.getLabel_id(),
                    blogArticle.getImage(),new String[]{blogArticle.getTag()},blogArticle.getOverview(),
                    DateUtils.parseDate(blogArticle.getCreate_time(),"yyyy-MM-dd HH:mm:ss"));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        UpdateRequest updateRequest=new UpdateRequest("blog",blogArticle.getId().toString());
        updateRequest.doc(JSON.toJSONString(blogEs), XContentType.JSON);
        UpdateResponse updateResponse=null;
        try {
            updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String status = updateResponse.status().toString();
        if ("OK".equals(status))
            return true;
        return false;
    }

    @Override
    public boolean deleteDoc(String blogId) {
        DeleteRequest deleteRequest=new DeleteRequest("blog",blogId);
        try {
            DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
            log.info(delete.status().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

}
